package com.zhijiaoyun.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.common.Constant;
import com.zhijiaoyun.common.ResponseUtils;
import com.zhijiaoyun.dao.INewsDao;
import com.zhijiaoyun.dao.INewsRecommendDao;
import com.zhijiaoyun.model.NewsRecommend;
import com.zhijiaoyun.model.dto.NewsDto;
import com.zhijiaoyun.model.dto.NewsRecommendDto;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.INewsRecommendService;
import com.zhijiaoyun.service.INewsService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "newsRecommendService")
public class NewsRecommendServiceImpl implements INewsRecommendService {

	@Resource
	INewsRecommendDao newsRecommendDao;

	@Resource
	INewsDao newsDao;
	
	@Resource 
	INewsService newsService;
	
	@Override
	public NewsRecommend getNewsRecommendByNewsId(Long newsId) {
		return newsRecommendDao.selectByNewsId(newsId);
	}

	@Override
	public void addNewsRecommend(NewsRecommend newsRecommend) {
		newsRecommendDao.insert(newsRecommend);
	}

	@Override
	public void deleteNewsRecommend(NewsRecommend newsRecommend) {
		newsRecommendDao.deleteByPrimaryKey(newsRecommend.getRecommendId());
	}

	@Override
	public List<NewsRecommendDto> getNewsRecommentDtoByParams() {
		List<NewsRecommendDto> newsRecommendDtos = new ArrayList<>();
		return newsRecommendDtos;
	}

	@Override
	public List<NewsRecommendDto> getNewsRecommentByParamsDto(int pageIndex, int pageSize) {
		List<NewsRecommend> newsRecommends = newsRecommendDao.getByParamsDto((pageIndex - 1) * pageSize, pageSize);
		List<NewsRecommendDto> newsRecommendDtos = new ArrayList<>();
		if (!StringUtils.isEmptyList(newsRecommends)) {
			for (NewsRecommend newsRecommend : newsRecommends) {
				NewsDto newsDto = newsService.getNewsDtoByNewsId(newsRecommend.getNewsId());
				if (newsDto != null) {
					NewsRecommendDto newsRecommendDto = new NewsRecommendDto(newsRecommend, newsDto);
					newsRecommendDtos.add(newsRecommendDto);
				}
				
			}
		}
		return newsRecommendDtos;
	}

	@Override
	public PageList<NewsRecommendDto> pageNewsRecommentDtoByParams(int pageIndex, int pageSize) {
		int count = newsRecommendDao.countByParams();
		PageList<NewsRecommendDto> newsRecommendPage = new PageList<>();
		if (count > 0) {
			PageData page = PageData.createPage(count, pageIndex, pageSize);
			List<NewsRecommendDto> newsRecommendDtos = new ArrayList<>();
			List<NewsRecommend> newsRecommends = newsRecommendDao.pageByParams(page.getBeginIndex(), page.getPageSize());
			if (!StringUtils.isEmptyList(newsRecommends)) {
				for (NewsRecommend newsRecommend : newsRecommends) {
					NewsDto newsDto = newsService.getNewsDtoByNewsId(newsRecommend.getNewsId());
					if (newsDto != null) {
						NewsRecommendDto newsRecommendDto = new NewsRecommendDto(newsRecommend, newsDto);
						newsRecommendDtos.add(newsRecommendDto);
					}
				}
			}
			newsRecommendPage.setList(newsRecommendDtos);
			newsRecommendPage.setPageData(page);
		}
		
		return newsRecommendPage;
	}

	@Override
	public void deleteByNewsId(long newsId) {
		NewsRecommend newsRecommend = getNewsRecommendByNewsId(newsId);
		if (newsRecommend != null) {
			newsRecommendDao.deleteByPrimaryKey(newsRecommend.getRecommendId());
		}
	}

	@Override
	public Map<String, Object> updateRecommend(long newsId, HttpServletRequest request) {
		String createUser = String.valueOf(request.getAttribute(Constant.CREATE_USER));
		NewsRecommend newsRecommend = getNewsRecommendByNewsId(newsId);
		if (newsRecommend == null) {
			newsRecommend = new NewsRecommend(newsId, "", null, createUser);
			addNewsRecommend(newsRecommend);
		} else {
			deleteNewsRecommend(newsRecommend);
		}
		return ResponseUtils.responseSuccessMap();
	}

	@Override
	public Map<String, Object> page(int pageIndex, int pageSize) {
		PageList<NewsRecommendDto> news = pageNewsRecommentDtoByParams(pageIndex, pageSize);
		Map<String, Object> map = new HashMap<>();
		map.put("list", news.getList());
		map.put("page", news.getPageData());
		return ResponseUtils.responseSuccessMap(map);
	}

}
